package com.dlnu.sujiwords.service.Impl;

import com.dlnu.sujiwords.dao.Memories_numberMapper;
import com.dlnu.sujiwords.domain.client.AjaxResult;
import com.dlnu.sujiwords.domain.pojo.Memories_number;
import com.dlnu.sujiwords.service.IMemoriesNumberService;
import com.dlnu.sujiwords.util.constant.EnWordsConstant;
import com.dlnu.sujiwords.util.table.EnTableName;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MemoriesNumberServiceImpl implements IMemoriesNumberService {
    @Resource
    private Memories_numberMapper mapper;

    @Override
    public AjaxResult getUserMemoriesNumber(String phoneNumber) {
        return AjaxResult.success("查询成功", mapper.selectByPhoneNumber(phoneNumber));
    }

    /*更新用户记忆数量*/
    @Override
    public AjaxResult updateUserMemoriesNum(String phoneNumber, int tableNo, int memorizeNum) {
        String colName = EnTableName.toMemorizesRowName(tableNo);
        int now = mapper.selectTableNumByTableNumAndPhoneNumber(phoneNumber, colName) + memorizeNum;
        now = correctMemorizesNumber(now, tableNo);
        return now > 0 && mapper.updateUserMemoriesNumber(phoneNumber, Integer.valueOf(now), colName) > 0 ? AjaxResult.success("已更新记忆数量") : AjaxResult.success("更新异常");
    }

    /*获取用户具体记忆数量*/
    @Override
    public AjaxResult getUserSpecificMemorizesNumber(String phoneNumber, int tableNo) {
        Memories_number numbers = mapper.selectByPhoneNumber(phoneNumber);
        int res;
        switch (tableNo) {
            case 0:
                res = numbers.getSchoolNum();
                break;
            case 1:
                res = numbers.getHighSchoolNum();
                break;
            case 2:
                res = numbers.getCet4Num();
                break;
            case 3:
                res = numbers.getCet6Num();
                break;
            case 4:
                res = numbers.getPostgraduataWordNum();
                break;
            default:
                res = -1;
        }

        return res>=0?AjaxResult.success("查询成功", res):AjaxResult.success("数据异常");
    }

    /*确保更新的数据正确且不大于词表总单词数*/
    public int correctMemorizesNumber(int now, int tableNo) {
        switch (tableNo) {
            case 0: {
                return now < EnWordsConstant.SCHOOLS_NUMS ? now : EnWordsConstant.SCHOOLS_NUMS;
            }
            case 1: {
                return now < EnWordsConstant.HIGH_SCHOOL_NUMS ? now : EnWordsConstant.HIGH_SCHOOL_NUMS;
            }
            case 2: {
                return now < EnWordsConstant.CET_4_NUMS ? now : EnWordsConstant.CET_4_NUMS;
            }
            case 3: {
                return now < EnWordsConstant.CET_6_NUMS ? now : EnWordsConstant.CET_6_NUMS;
            }
            case 4: {
                return now < EnWordsConstant.POSTGRADUATA_WORD_NUMS ? now : EnWordsConstant.POSTGRADUATA_WORD_NUMS;
            }
            case 5: {
                return now < EnWordsConstant.WORDS_NUMS ? now : EnWordsConstant.WORDS_NUMS;
            }
            default: {
                return -1;
            }
        }
    }

}
